<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Crm extends MX_Controller {
    
  function __construct() {
    $this->load->library('manimage');
    $this->menu = $this->cek();
  }
	
	public function master_market_target(){
    if($this->input->post()){
      if($this->input->post('cari') == 's'){
        $session_cari = array(
            "crm_market_target_search_title"        => $this->input->post("s_title"),
            "crm_market_target_search_up_to"        => $this->input->post("s_up_to"),
            "crm_market_target_search_email"        => $this->input->post("s_email"),
            "crm_market_target_search_telphone"     => $this->input->post("s_telphone"),
        );
        $this->session->set_userdata($session_cari);
      }
      else{
        $session_cari = array(
            "crm_market_target_search_title"        => "",
            "crm_market_target_search_up_to"        => "",
            "crm_market_target_search_email"        => "",
            "crm_market_target_search_telphone"     => "",
        );
        $this->session->unset_userdata($session_cari);
      }
      redirect("crm/master-market-target");
    }
    
    $where = "WHERE 1 = 1 ";
    if($this->session->userdata("crm_market_target_search_title"))
      $where .= "AND LOWER(title) LIKE '%".strtolower($this->session->userdata("crm_market_target_search_title"))."%' ";
    
    if($this->session->userdata("crm_market_target_search_up_to"))
      $where .= "AND LOWER(up_to) LIKE '%".strtolower($this->session->userdata("crm_market_target_search_up_to"))."%' ";
    
    if($this->session->userdata("crm_market_target_search_email"))
      $where .= "AND LOWER(email) LIKE '%".strtolower($this->session->userdata("crm_market_target_search_email"))."%' ";
    
    if($this->session->userdata("crm_market_target_search_telphone"))
      $where .= "AND LOWER(telphone) LIKE '%".strtolower($this->session->userdata("crm_market_target_search_telphone"))."%' ";
    
    $total = $this->global_models->get_query("
      SELECT count(id_crm_market_target) AS jumlah
      FROM crm_market_target
      {$where}
      ");
    $this->template->build("master-market-target",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'crm/master-market-target',
            'title'       => lang("master_market_target"),
            'total'       => $total[0]->jumlah
          ));
    $this->template
      ->set_layout('default')
      ->build("master-market-target");
	}
	
	public function master_products(){
    if($this->input->post()){
      if($this->input->post('cari') == 's'){
        $session_cari = array(
            "crm_market_target_search_title"        => $this->input->post("s_title"),
            "crm_market_target_search_up_to"        => $this->input->post("s_up_to"),
            "crm_market_target_search_email"        => $this->input->post("s_email"),
            "crm_market_target_search_telphone"     => $this->input->post("s_telphone"),
        );
        $this->session->set_userdata($session_cari);
      }
      else{
        $session_cari = array(
            "crm_market_target_search_title"        => "",
            "crm_market_target_search_up_to"        => "",
            "crm_market_target_search_email"        => "",
            "crm_market_target_search_telphone"     => "",
        );
        $this->session->unset_userdata($session_cari);
      }
      redirect("crm/master-market-target");
    }
    
    $where = "WHERE 1 = 1 ";
    if($this->session->userdata("crm_market_target_search_title"))
      $where .= "AND LOWER(title) LIKE '%".strtolower($this->session->userdata("crm_market_target_search_title"))."%' ";
    
    if($this->session->userdata("crm_market_target_search_up_to"))
      $where .= "AND LOWER(up_to) LIKE '%".strtolower($this->session->userdata("crm_market_target_search_up_to"))."%' ";
    
    if($this->session->userdata("crm_market_target_search_email"))
      $where .= "AND LOWER(email) LIKE '%".strtolower($this->session->userdata("crm_market_target_search_email"))."%' ";
    
    if($this->session->userdata("crm_market_target_search_telphone"))
      $where .= "AND LOWER(telphone) LIKE '%".strtolower($this->session->userdata("crm_market_target_search_telphone"))."%' ";
    
    $total = $this->global_models->get_query("
      SELECT count(id_crm_products) AS jumlah
      FROM crm_products
      {$where}
      ");
    $this->template->build("master-products",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'crm/master-products',
            'title'       => lang("products"),
            'total'       => $total[0]->jumlah
          ));
    $this->template
      ->set_layout('default')
      ->build("master-products");
	}
	
	public function history_market_target(){
    if($this->input->post()){
      if($this->input->post('cari') == 's'){
        $session_cari = array(
            "crm_market_target_search_title"        => $this->input->post("s_title"),
            "crm_market_target_search_up_to"        => $this->input->post("s_up_to"),
            "crm_market_target_search_email"        => $this->input->post("s_email"),
            "crm_market_target_search_telphone"     => $this->input->post("s_telphone"),
        );
        $this->session->set_userdata($session_cari);
      }
      else{
        $session_cari = array(
            "crm_market_target_search_title"        => "",
            "crm_market_target_search_up_to"        => "",
            "crm_market_target_search_email"        => "",
            "crm_market_target_search_telphone"     => "",
        );
        $this->session->unset_userdata($session_cari);
      }
      redirect("crm/master-market-target");
    }
    
    $where = "WHERE 1 = 1 ";
    if($this->session->userdata("crm_market_target_search_title"))
      $where .= "AND LOWER(title) LIKE '%".strtolower($this->session->userdata("crm_market_target_search_title"))."%' ";
    
    if($this->session->userdata("crm_market_target_search_up_to"))
      $where .= "AND LOWER(up_to) LIKE '%".strtolower($this->session->userdata("crm_market_target_search_up_to"))."%' ";
    
    if($this->session->userdata("crm_market_target_search_email"))
      $where .= "AND LOWER(email) LIKE '%".strtolower($this->session->userdata("crm_market_target_search_email"))."%' ";
    
    if($this->session->userdata("crm_market_target_search_telphone"))
      $where .= "AND LOWER(telphone) LIKE '%".strtolower($this->session->userdata("crm_market_target_search_telphone"))."%' ";
    
    $total = $this->global_models->get_query("
      SELECT count(id_crm_market_target) AS jumlah
      FROM crm_market_target
      {$where}
      ");
    $this->template->build("history-market-target",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'crm/master-market-target',
            'title'       => lang("master_market_target"),
            'total'       => $total[0]->jumlah
          ));
    $this->template
      ->set_layout('default')
      ->build("history-market-target");
	}
	
  public function ajax_products($total = 0, $start = 0){
    
    $where = "WHERE 1 = 1 ";
    if($this->session->userdata("crm_market_target_search_title"))
      $where .= "AND LOWER(title) LIKE '%".strtolower($this->session->userdata("crm_market_target_search_title"))."%' ";
    
    if($this->session->userdata("crm_market_target_search_up_to"))
      $where .= "AND LOWER(up_to) LIKE '%".strtolower($this->session->userdata("crm_market_target_search_up_to"))."%' ";
    
    if($this->session->userdata("crm_market_target_search_email"))
      $where .= "AND LOWER(email) LIKE '%".strtolower($this->session->userdata("crm_market_target_search_email"))."%' ";
    
    if($this->session->userdata("crm_market_target_search_telphone"))
      $where .= "AND LOWER(telphone) LIKE '%".strtolower($this->session->userdata("crm_market_target_search_telphone"))."%' ";
    
    $data = $this->global_models->get_query("
      SELECT A.*, B.name AS category, C.name AS brand
      FROM crm_products AS A
      LEFT JOIN crm_product_category AS B ON A.id_crm_product_category = B.id_crm_product_category
      LEFT JOIN crm_product_brand AS C ON A.id_crm_product_brand = C.id_crm_product_brand
      {$where}
      ORDER BY A.title ASC LIMIT {$start},10
      ");
    
    foreach ($data as $key => $value) {
      
      print "
      <tr class='ajax-target'>
        <td><img src='".base_url()."/files/crm/products/50/".$value->picture."' /></td>
        <td>{$value->title}</td>
        <td>{$value->code}</td>
        <td>{$value->category}</td>
        <td>{$value->brand}</td>
        <td>
          <div class='btn-group'>
            <button data-toggle='dropdown' class='btn btn-small dropdown-toggle'>Action<span class='caret'></span></button>
            <ul class='dropdown-menu'>
              <li><a href='".site_url("crm/add-products/".$value->id_crm_products)."'>Edit</a></li>
              <li><a href='".site_url("crm/delete-products/".$value->id_crm_products)."'>Delete</a></li>
            </ul>
          </div>
        </td>
      </tr>";
        
    }
    
    $this->load->library('pagination');

    $config['base_url'] = '';
    $config['total_rows'] = $total;
    $config['per_page'] = 10; 
    $config['uri_segment'] = 4; 
    $config['cur_tag_open'] = "<li class='active'><a href='javascript:void(0)'>"; 
    $config['cur_tag_close'] = "</a></li>"; 
    $config['first_tag_open'] = "<li>"; 
    $config['first_tag_close'] = "</li>"; 
    $config['last_tag_open'] = "<li>"; 
    $config['last_tag_close'] = "</li>"; 
    $config['next_tag_open'] = "<li>"; 
    $config['next_tag_close'] = "</li>"; 
    $config['prev_tag_open'] = "<li>"; 
    $config['prev_tag_close'] = "</li>"; 
    $config['num_tag_open'] = "<li>"; 
    $config['num_tag_close'] = "</li>";
    $config['function_js'] = "load_products";

    $this->pagination->initialize($config); 
    print "
    <tr class='ajax-products'>
      <td colspan='6'>
        <div class='pagination'>
          <ul>
          ".$this->pagination->create_links_ajax()."
          </ul>
        </div>
      </td>
    </tr>";
    die;
  }
  
  public function ajax_market_target($total = 0, $start = 0){
    
    $where = "WHERE 1 = 1 ";
    if($this->session->userdata("crm_market_target_search_title"))
      $where .= "AND LOWER(title) LIKE '%".strtolower($this->session->userdata("crm_market_target_search_title"))."%' ";
    
    if($this->session->userdata("crm_market_target_search_up_to"))
      $where .= "AND LOWER(up_to) LIKE '%".strtolower($this->session->userdata("crm_market_target_search_up_to"))."%' ";
    
    if($this->session->userdata("crm_market_target_search_email"))
      $where .= "AND LOWER(email) LIKE '%".strtolower($this->session->userdata("crm_market_target_search_email"))."%' ";
    
    if($this->session->userdata("crm_market_target_search_telphone"))
      $where .= "AND LOWER(telphone) LIKE '%".strtolower($this->session->userdata("crm_market_target_search_telphone"))."%' ";
    
    $data = $this->global_models->get_query("
      SELECT *
      FROM crm_market_target
      {$where}
      ORDER BY title ASC LIMIT {$start},10
      ");
    
    foreach ($data as $key => $value) {
      
      print "
      <tr class='ajax-target'>
        <td>{$value->title}</td>
        <td>{$value->up_to}</td>
        <td>{$value->email}</td>
        <td>{$value->telphone}</td>
        <td>
          <div class='btn-group'>
            <button data-toggle='dropdown' class='btn btn-small dropdown-toggle'>Action<span class='caret'></span></button>
            <ul class='dropdown-menu'>
              <li><a href='".site_url("crm/add-market-target/".$value->id_crm_market_target)."'>Edit</a></li>
              <li><a href='".site_url("crm/delete-market-target/".$value->id_crm_market_target)."'>Delete</a></li>
            </ul>
          </div>
        </td>
      </tr>";
        
    }
    
    $this->load->library('pagination');

    $config['base_url'] = '';
    $config['total_rows'] = $total;
    $config['per_page'] = 10; 
    $config['uri_segment'] = 4; 
    $config['cur_tag_open'] = "<li class='active'><a href='javascript:void(0)'>"; 
    $config['cur_tag_close'] = "</a></li>"; 
    $config['first_tag_open'] = "<li>"; 
    $config['first_tag_close'] = "</li>"; 
    $config['last_tag_open'] = "<li>"; 
    $config['last_tag_close'] = "</li>"; 
    $config['next_tag_open'] = "<li>"; 
    $config['next_tag_close'] = "</li>"; 
    $config['prev_tag_open'] = "<li>"; 
    $config['prev_tag_close'] = "</li>"; 
    $config['num_tag_open'] = "<li>"; 
    $config['num_tag_close'] = "</li>";
    $config['function_js'] = "load_target";

    $this->pagination->initialize($config); 
    print "
    <tr class='ajax-target'>
      <td colspan='5'>
        <div class='pagination'>
          <ul>
          ".$this->pagination->create_links_ajax()."
          </ul>
        </div>
      </td>
    </tr>";
    die;
  }
  
  function delete_product_category($id_crm_product_category){
    $this->global_models->delete("crm_product_category", array("id_crm_product_category" => $id_crm_product_category));
    redirect("crm/master-product-category");
  }
  
  public function add_market_target($id_crm_market_target = 0){
    if($this->input->post()){
      $pst = $this->input->post();
      if($pst['id_detail']){
        $kirim = array(
            "title"             => $pst['title'],
            "up_to"             => $pst['up_to'],
            "email"             => $pst['email'],
            "telphone"          => $pst['telphone'],
            "fax"               => $pst['fax'],
            "note"              => $pst['note'],
            "update_by_users"   => $this->session->userdata("id"),
        );
        $id_crm_market_target = $this->global_models->update("crm_market_target", array("id_crm_market_target" => $pst['id_detail']), $kirim);
      }
      else{
        $kirim = array(
            "title"             => $pst['title'],
            "up_to"             => $pst['up_to'],
            "email"             => $pst['email'],
            "telphone"          => $pst['telphone'],
            "fax"               => $pst['fax'],
            "note"              => $pst['note'],
            "create_by_users"   => $this->session->userdata("id"),
            "create_date"       => date("Y-m-d H:i:s")
        );
        $id_crm_market_target = $this->global_models->insert("crm_market_target", $kirim);
      }
      if($id_crm_market_target)
        redirect ("crm/master-market-target/1/Berhasil");
      else
        redirect ("crm/master-market-target/2/Gagal");
    }
    else{
      $detail = $this->global_models->get("crm_market_target", array("id_crm_market_target" => $id_crm_market_target));
      $this->template->build("add-market-target",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'crm/master-market-target',
              'title'       => lang("master_market_target"),
              'detail'      => $detail,
            ));
      $this->template
        ->set_layout('default')
        ->build("add-market-target");
    }
	}
  
  public function add_products($id = 0){
    if(!$this->input->post(NULL, TRUE)){
      $detail = $this->global_models->get("crm_products", array("id_crm_products" => $id));
      
      $parent = $this->global_models->get_dropdown("crm_product_category", "id_crm_product_category", "name", FALSE, array("id_parent <" => 1));
      $brand = $this->global_models->get_dropdown("crm_product_brand", "id_crm_product_brand", "name", FALSE);
      
      $this->template->build('add-products', 
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'crm/master-products',
              'title'       => lang("products"),
              'total'       => $total[0]->jumlah,
              'parent'      =>  $parent,
              'brand'       =>  $brand,
              'detail'      => $detail,
            ));
      $this->template
        ->set_layout('default')
        ->build('add-products');
    }
    else{
      if($this->input->post("id_detail", TRUE)){
        $pst = $this->input->post(NULL, TRUE);
        $config['upload_path'] = './files/crm/products/';
        $config['allowed_types'] = 'gif|jpg|png';
//        $config['max_size']	= '100';
        $config['max_width']  = '1168';
        $config['max_height']  = '885';

        $this->load->library('upload', $config);
//        $this->debug($_FILES, true);
        if (  $this->upload->do_upload('picture')){
//          $error = array('error' => $this->upload->display_errors());
//          print $this->upload->display_errors();
//          die;
//        }
//        else{
          $data = array('upload_data' => $this->upload->data());
          $this->manimage->load('./files/crm/products/'.$data['upload_data']['file_name']); 
          $this->manimage->resizeToWidth(590); 
          $this->manimage->save('./files/crm/products/'.$data['upload_data']['file_name']);
          
          $this->manimage->load('./files/crm/products/'.$data['upload_data']['file_name']); 
          $this->manimage->resizeToWidth(270); 
          $this->manimage->save('./files/crm/products/270/'.$data['upload_data']['file_name']);
          
          $this->manimage->load('./files/crm/products/'.$data['upload_data']['file_name']); 
          $this->manimage->resizeToWidth(50); 
          $this->manimage->save('./files/crm/products/50/'.$data['upload_data']['file_name']);
        }
        
        $nicename = $this->global_models->nicename($pst['title']);
        if($this->global_models->get_field("crm_products", "id_crm_products", array('nicename' => $nicename, "id_crm_products <>" => $pst['id_detail']))){
          $nicecode = $this->global_models->nicename($pst['code']);
          $nicename .= "_".$nicecode;
        }
        $kirim = array(
            'title'           =>  $pst['title'],
            'nicename'        =>  $nicename,
            'code'            =>  $pst['code'],
            'id_crm_product_category'     =>  $pst['id_crm_product_category'],
            'id_crm_product_brand'        =>  $pst['id_crm_product_brand'],
            'price'           =>  $pst['price'],
            'weight'          =>  $pst['weight'],
            'discount'        =>  $pst['discount'],
            'note'            =>  $pst['note'],
            'update_by_users' =>  $this->session->userdata("id")
        );
        if($data['upload_data']['file_name']){
          $kirim['picture'] = $data['upload_data']['file_name'];
        }
        
        $hasil_in = $this->global_models->update("crm_products", array("id_crm_products" => $pst['id_detail']), $kirim);
        if($hasil_in)
          redirect ("crm/master-products/sukses");
        else
          redirect ("crm/master-products/filed");
      }
      else{
        $pst = $this->input->post(NULL, TRUE);
        
        $config['upload_path'] = './files/crm/products/';
        $config['allowed_types'] = 'gif|jpg|png';
//        $config['max_size']	= '100';
        $config['max_width']  = '1168';
        $config['max_height']  = '768';

        $this->load->library('upload', $config);
//        $this->debug($_FILES, true);
        if($_FILES['picture']['name']){
          if ( ! $this->upload->do_upload('picture')){
            $error = array('error' => $this->upload->display_errors());
            print $this->upload->display_errors();
            die;
          }
          else{
            $data = array('upload_data' => $this->upload->data());
            $this->manimage->load('./files/crm/products/'.$data['upload_data']['file_name']); 
            $this->manimage->resizeToWidth(590); 
            $this->manimage->save('./files/crm/products/'.$data['upload_data']['file_name']);

            $this->manimage->load('./files/crm/products/'.$data['upload_data']['file_name']); 
            $this->manimage->resizeToWidth(270); 
            $this->manimage->save('./files/crm/products/270/'.$data['upload_data']['file_name']);

            $this->manimage->load('./files/crm/products/'.$data['upload_data']['file_name']); 
            $this->manimage->resizeToWidth(50); 
            $this->manimage->save('./files/crm/products/50/'.$data['upload_data']['file_name']);
          }
        }
        
        $nicename = $this->global_models->nicename($pst['title']);
        if($this->global_models->get_field("crm_products", "id_crm_products", array('nicename' => $nicename))){
          $nicecode = $this->global_models->nicename($pst['code']);
          $nicename .= "_".$nicecode;
        }
        
        $kirim = array(
            'title'           =>  $pst['title'],
            'nicename'        =>  $nicename,
            'code'            =>  $pst['code'],
            'id_crm_product_category'     =>  $pst['id_crm_product_category'],
            'id_crm_product_brand'        =>  $pst['id_crm_product_brand'],
            'price'           =>  $pst['price'],
            'weight'          =>  $pst['weight'],
            'discount'        =>  $pst['discount'],
            'note'            =>  $pst['note'],
            'create_by_users' =>  $this->session->userdata("id"),
            'create_date'     =>  date("Y-m-d H:i:s")
        );
        if($data['upload_data']['file_name']){
          $kirim['picture'] = $data['upload_data']['file_name'];
        }
        
        $hasil_in = $this->global_models->insert("crm_products", $kirim);
        if($hasil_in)
          redirect ("crm/master-products/sukses");
        else
          redirect ("crm/master-products/filed");
      }
    }
  }
  
  public function master_product_brand(){
    $data = $this->global_models->get("crm_product_brand");
    $this->template->build('master-product-brand',
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'crm/master-product-brand',
            'title'       => lang("product_brand"),
            'data'        => $data,
          ));
    $this->template
      ->set_layout('default')
      ->build('master-product-brand');
	}
  
  public function add_product_brand($id = 0){
    $detail = $this->global_models->get("crm_product_brand", array("id_crm_product_brand" => $id));
    if(!$this->input->post(NULL, TRUE)){
      $this->template->build('add-product-brand', 
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'crm/master-product-brand',
              'title'       => lang("product_brand"),
              'detail'      => $detail,
            ));
      $this->template
        ->set_layout('default')
        ->build('add-product-brand');
    }
    else{
      if($this->input->post("id_detail", TRUE)){
        $kirim = array(
              "name"                  =>  $this->input->post("name", TRUE),
              "note"                  =>  $this->input->post("note", TRUE),
              "nicename"              => $this->global_models->nicename($this->input->post("name", TRUE)),
              "update_by_users"       =>  $this->session->userdata('id')
          );
        $hasil_in = $this->global_models->update("crm_product_brand", array("id_crm_product_brand" => $this->input->post("id_detail", TRUE)), $kirim);
        if($hasil_in){
          redirect ("crm/master-product-brand/1/Gagal");
        }
        else
          redirect ("crm/master-product-brand/2/Gagal");
      }
      else{
        $pst = $this->input->post(NULL, TRUE);
//        $pst = $pst['addressform']['addressform'][0];
        if($pst['name']){
          $kirim = array(
              "name"                  =>  $pst['name'],
              "note"                  =>  $pst['note'],
              "nicename"              => $this->global_models->nicename($pst["name"]),
              "create_by_users"       =>  $this->session->userdata('id'),
              "create_date"           =>  date("Y-m-d H:i:s"),
          );
          if($this->global_models->insert("crm_product_brand", $kirim))
            redirect ("crm/master-product-brand/1/Berhasil");
          else{
            redirect ("crm/master-product-brand/2/Gagal");
          }
        }
        else{
          redirect ("crm/master-product-brand/2/Gagal");
        }
      }
    }
  }
  
  public function master_product_category(){
    $data = $this->global_models->get_query("
      SELECT A.*, B.name AS parent
      FROM crm_product_category AS A
      LEFT JOIN crm_product_category AS B ON B.id_crm_product_category = A.id_parent
      ");
    $this->template->build('master-product-category', 
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'crm/master-product-category',
            'title'       => lang("product_category"),
            'data'        => $data,
          ));
    $this->template
      ->set_layout('default')
      ->build('master-product-category');
	}
  
  public function add_product_category($id = 0){
    $detail = $this->global_models->get("crm_product_category", array("id_crm_product_category" => $id));
    $parent = $this->global_models->get_dropdown("crm_product_category", "id_crm_product_category", "name", true, array("id_parent <" => 1));
    if(!$this->input->post(NULL, TRUE)){
      $this->template->build('add-product-category', 
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'crm/master-product-category',
              'title'       => lang("product_category"),
              'detail'      => $detail,
              'parent'      => $parent,
            ));
      $this->template
        ->set_layout('default')
        ->build('add-product-category');
    }
    else{
      if($this->input->post("id_detail", TRUE)){
        $kirim = array(
              "name"                  =>  $this->input->post("name", TRUE),
              "nicename"              => $this->global_models->nicename($this->input->post("name", TRUE)),
              "note"                  =>  $this->input->post("note", TRUE),
              "id_parent"             =>  $this->input->post("id_parent", TRUE),
              "update_by_users"       =>  $this->session->userdata('id')
          );
        $hasil_in = $this->global_models->update("crm_product_category", array("id_crm_product_category" => $this->input->post("id_detail", TRUE)), $kirim);
        if($hasil_in){
          redirect ("crm/master-product-category/1/Gagal");
        }
        else
          redirect ("crm/master-product-category/2/Gagal");
      }
      else{
        $pst = $this->input->post(NULL, TRUE);
//        $pst = $pst['addressform']['addressform'][0];
        if($pst['name']){
          $kirim = array(
              "name"                  =>  $pst['name'],
              "nicename"              => $this->global_models->nicename($pst["name"]),
              "note"                  =>  $pst['note'],
              "id_parent"             =>  $pst["id_parent"],
              "create_by_users"       =>  $this->session->userdata('id'),
              "create_date"           =>  date("Y-m-d H:i:s"),
          );
          if($this->global_models->insert("crm_product_category", $kirim))
            redirect ("crm/master-product-category/1/Berhasil");
          else{
            redirect ("crm/master-product-category/2/Gagal");
          }
        }
        else{
          redirect ("crm/master-product-category/2/Gagal");
        }
      }
    }
  }
  
  function auto_target_market(){
    if (empty($_GET['term'])) exit ;
    $q = strtolower($_GET["term"]);
    if (get_magic_quotes_gpc()) $q = stripslashes($q);
    $items = $this->global_models->get_query("
      SELECT *
      FROM crm_market_target
      WHERE LOWER(title) LIKE '%{$q}%'
      LIMIT 0,10
      ");
    foreach($items as $tms){
      $result[] = array(
          "id"    => $tms->id_crm_market_target,
          "label" => $tms->title,
          "value" => $tms->title,
      );
    }
//    $result = array();
//    foreach ($items as $key=>$value) {
//      if (strpos(strtolower($key), $q) !== false) {
//        array_push($result, array("id"=>$value, "label"=>$key, "value" => strip_tags($key)));
//      }
//      if (count($result) > 11)
//        break;
//    }
    echo json_encode($result);
    die;
  }
  
  function auto_products(){
    if (empty($_GET['term'])) exit ;
    $q = strtolower($_GET["term"]);
    if (get_magic_quotes_gpc()) $q = stripslashes($q);
    $items = $this->global_models->get_query("
      SELECT *
      FROM crm_products
      WHERE LOWER(title) LIKE '%{$q}%'
      LIMIT 0,10
      ");
    foreach($items as $tms){
      $result[] = array(
          "id"    => $tms->id_crm_products,
          "label" => $tms->title,
          "value" => $tms->title,
      );
    }
    echo json_encode($result);
    die;
  }
  
  function auto_field_products(){
    $id_crm_products = $this->input->post("id");
    $data = $this->global_models->get("crm_products", array("id_crm_products" => $id_crm_products));
    $hasil = array(
        "price"     => $data[0]->price,
        "discount"  => $data[0]->discount,
    );
    print json_encode($hasil);
    die;
  }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */